package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

    @Autowired
    private BaseCategory1Mapper baseCategory1Mapper;

    @Autowired
    private BaseCategory2Mapper baseCategory2Mapper;

    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;


    @Autowired
    private BaseCategoryViewMapper baseCategoryViewMapper;

    @Autowired
    private BaseAttributeMapper baseAttributeMapper;
    @Autowired
    private BaseAttributeValueMapper baseAttributeValueMapper;


    @Override
    public List<JSONObject> getBaseCategoryList() {
        //开始实现代码，其实本身自己也想返回Map的，但是事实上他用的JsonObject，更为规范
        /*
         *   明确一下Map的数据结构首先是一级分类里面有List<二级分类>,二级分类里有List<三级分类这样>
         *
         */

        List<BaseCategory1> baseCategory1List = baseCategory1Mapper.selectList(null);
        List<JSONObject> categoryList = baseCategory1List.stream().map(baseCategory1 -> {

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("categoryName", baseCategory1.getName());
            jsonObject.put("categoryId", baseCategory1.getId());

            List<BaseCategory2> baseCategory2List = baseCategory2Mapper
                    .selectList(new LambdaQueryWrapper<BaseCategory2>()
                            .eq(BaseCategory2::getCategory1Id, baseCategory1.getId()));
            List<JSONObject> jsonObjectList2 = baseCategory2List.stream().map(baseCategory2 -> {
                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.put("categoryName", baseCategory2.getName());
                jsonObject2.put("categoryId", baseCategory2.getId());

                List<BaseCategory3> baseCategory3List = baseCategory3Mapper
                        .selectList(new LambdaQueryWrapper<BaseCategory3>()
                                .eq(BaseCategory3::getCategory2Id, baseCategory2.getId()));

                List<JSONObject> jsonObjectList3 = baseCategory3List.stream().map(baseCategory3 -> {
                    JSONObject jsonObject3 = new JSONObject();
                    jsonObject3.put("categoryName", baseCategory3.getName());
                    jsonObject3.put("categoryId", baseCategory3.getId());
                    return jsonObject3;
                }).toList();
                jsonObject2.put("categoryChild", jsonObjectList3);
                return jsonObject2;
            }).toList();


            jsonObject.put("categoryChild", jsonObjectList2);

            return jsonObject;
        }).toList();


        return categoryList;
    }

    @Override
    public List<BaseAttribute> findAttribute(Long category1Id) {

        List<BaseAttribute> baseAttributes = baseAttributeMapper.selectList(
                new LambdaQueryWrapper<BaseAttribute>()
                        .eq(BaseAttribute::getCategory1Id, category1Id));

        baseAttributes.stream().forEach(
                baseAttribute -> {
                    LambdaQueryWrapper<BaseAttributeValue> queryWrapper = new LambdaQueryWrapper<>();
                    queryWrapper.eq(BaseAttributeValue::getAttributeId, baseAttribute.getId());
                    List<BaseAttributeValue> baseAttributeValues = baseAttributeValueMapper.selectList(queryWrapper);
                    baseAttribute.setAttributeValueList(baseAttributeValues);

                }
        );



        return baseAttributes;
    }
}
